<?php
@$act = $_GET['act'];
@$id = $_GET['id'];
@$loai_sp = $_POST['selLoai'];
@$tieu_de_vi = $_POST['txtTitle_vi'];
@$tieu_de_en = $_POST['txtTitle_en'];
@$name_hinh = $_FILES['txtPic']['name'];
@$path_hinh = $_FILES['txtPic']['tmp_name'];
@$tt_nd_vi = $_POST['txtDesc_vi'];
@$tt_nd_en = $_POST['txtDesc_en'];
@$cont_vi = $_POST['txtCont_vi'];
@$cont_en = $_POST['txtCont_en'];
$cur_date = date('Y-m-d');
@$act_p = $_POST['txtEdit'];
@$id_p = $_POST['txtIdEdit'];


if(isset($_POST['btnSave']))
{
	@$pic = $exec_mysql->execute("select img from node where id = $id_p",0);
	@$rowPic = mysql_fetch_array($pic);
	//-------Upload anh-------------
	//echo 'file name:'.$path_hinh;
	if($name_hinh != "")
	{
	
	$uploaddir = 'category/'; 
    $ext = explode('.',$name_hinh);
    if($ext[1]=='gif' || $ext[1]=='GIF'|| $ext[1]=='jpg' || $ext[1]=='JPG' || $ext[1]=='png' || $ext[1]=='PNG'){
        $id=rand();
        $filename=$id.'.'.$ext[1];
        $uploadfile = $uploaddir . $filename;
		$size=round(filesize($path_hinh)/1000,2);
		if($size < 3000)
		{
    
			 if (move_uploaded_file($path_hinh, $uploadfile)) { 
                
                
                //echo 'Link file ảnh vừa upload: <font color="#FF0000">'.$uploadfile.'</font></br>';
				echo '<i>(Dung lượng file ảnh đính kèm: '.$size.' Kb)</i>';
                @$path_hinh_u = $uploadfile;
            } 
            else 
			{ 
				$path_hinh_u = $rowPic['img'];
                echo "<script>showAlert('Lỗi','Có lỗi xảy ra khi upload ảnh!','');</script>"; 
            }
		}
		else
		{
			$path_hinh_u = $rowPic['img'];
			echo "Không thể upload được ảnh. File ảnh upload phải nhỏ hơn 3Mb!";
		}
	 
    }
    else 
	{
		$path_hinh_u = $rowPic['img'];
        echo "Không thể upload được ảnh. File ảnh không đúng chuẩn (jpg, jpeg, gif, png)!";
    } 
	
	}
	else
	{
		@$path_hinh_u = $rowPic['img'];
		//echo '<br> Path hinh:'.$path_hinh_u;
		
	}
	
	//---------end-------------
	
	$val = $exec_mysql->escape_string(array($loai_sp, $path_hinh_u, $tieu_de_vi, $tieu_de_en, $cont_vi, $cont_en, $cur_date));
	
	if(@$act_p == 'edit')
	{
		
		if($rowPic['img'] != $path_hinh_u && $path_hinh_u != "" && $rowPic['img'] !="")
		{
			unlink($rowPic['img']);
		}
		$saved = $exec_mysql->execute("update node set id_ref = '$val[0]', img = '$val[1]', title_vi = '$val[2]', title_en = '$val[3]', content_vi = '$val[4]', content_en = '$val[5]' where id = $id_p", 0);
	}
	else 
	{
		$saved = $exec_mysql->execute("insert into node values ('','3','$val[0]','$val[1]','$val[2]','$val[3]','', '','$val[4]','$val[5]','$val[6]')",0);
	}
	if($saved == 1)
	{
		echo "<script>showAlert('Thông báo','Lưu dữ liệu thành công!','');</script>";
	}
	else
	{
		echo "<script>showAlert('Lỗi','Chưa thể lưu dữ liệu. Vui lòng kiểm tra lại!','');</script>";
	}
}

if(@$act == 'del')
{
	//echo 'xoa recode';
	$hinh = $exec_mysql->execute("select img from node where id = $id",0);
	$row = mysql_fetch_array($hinh);
	unlink($row['img']);
	$xoa = $exec_mysql->execute("delete from node where id = $id",0);
	echo "<script>";
		echo "window.location = 'admin_master_page.php?cmd=3.2'";
	echo "</script>";
}


function cut($str, $len) 
	{
		$str = trim($str);
		if (strlen($str) <= $len) return $str;
		$str = substr($str, 0, $len);
		if ($str != "") {
			if (!substr_count($str, " ")) return $str." ...";
			while (strlen($str) && ($str[strlen($str) - 1] != " ")) $str = substr($str, 0, -1);
			$str = substr($str, 0, -1)." ...";
		}
		return $str;
	}	
?>
<script type="text/javascript">
function deleletconfig(){

    var del=confirm("Bạn muốn xóa mẩu tin?");
    
    return del;
}
</script>

<p class="group-line">
   <span class="group-title">Sản phẩm đã lưu</span>
</p>

<table width="680px" class="table-list">
	<tr class="table-header">
        <td width="20px">STT</td>
        <td width="200px">Tiêu đề</td>
        <!--<td>Tóm tắt nội dung</td>--->
        <td width="45px">Cập nhật</td>
        <td width="45px">Xóa</td>
    </tr>
    <?php
	@$g_func = new global_function();
	@$cur_page = $_GET['page'];
	$ROWS_PER_PAGE = 10;
	$total_record = $exec_mysql->execute("select id from node where id_type = 3", 2);
	$begin = $g_func->get_begin($cur_page, $ROWS_PER_PAGE, $total_record);
	$result = $exec_mysql->execute("select * from node where id_type = 3 order by id_type, id desc limit $begin, $ROWS_PER_PAGE",0);
	$p = $begin + 1;
	while($row = mysql_fetch_array($result))
	{
		echo '<tr>';
			echo '<td align="center">'.$p++.'</td>';
			echo '<td>'.$row['title_vi'].' / '.$row['title_en'].'</td>';
			//echo '<td>'.cut($row['content_vi'],50).' / '.cut($row['content_en'],50).'</td>';
			echo '<td align="center"><a href="admin_master_page.php?cmd=3.1&id='.$row['id'].'&act=edit"><img src="img/ico_edit.png" /></a></td>';
			echo '<td align="center"><img src="img/ico_delete.png" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa không?\', \'admin_master_page.php?cmd=3.2&act=del&id='.$row['id'].'\')" class="del" /></td>';
		echo '</tr>';
	}
	?>
</table>

<?php
//=====phan trang===========================
//$g_func = new global_function();
$g_func->admin_paging($total_record,$ROWS_PER_PAGE);
?>


